home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / t_sys5 / unixcpio.gz / unixnet.cpio / nrsubr.c < prev    next >
C/C++ Source or Header  |  1994-07-11  |  3KB  |  151 lines

  1. /* Functions for level 3 net/rom support.
  2.  * Copyright 1989 by Daniel M. Frank, W9NK.  Permission granted for
  3.  * non-commercial distribution only.
  4.  */
  5.  
  6. #include "global.h"
  7. #include "mbuf.h"
  8. #include "timer.h"
  9. #include "ax25.h"
  10. #include "netrom.h"
  11. #include "lapb.h"
  12. #include <ctype.h>
  13. #ifdef    UNIX
  14. #include <memory.h>
  15. #endif
  16.  
  17. /* Convert a net/rom network header to host format structure
  18.  * Return -1 if error, 0 if OK
  19.  */
  20.  
  21. int
  22. ntohnr3(hdr,bpp)
  23. register struct nr3hdr *hdr ;    /* output structure */
  24. struct mbuf **bpp ;
  25. {
  26.     char *getaxaddr() ;
  27.     char buf[AXALEN] ;
  28.  
  29.     if (pullup(bpp,buf,AXALEN) < AXALEN)
  30.         return -1 ;
  31.     getaxaddr(&hdr->source,buf) ;
  32.  
  33.     if (pullup(bpp,buf,AXALEN) < AXALEN)
  34.         return -1 ;
  35.     getaxaddr(&hdr->dest,buf) ;
  36.  
  37.     if (pullup(bpp,(char *)&hdr->ttl,1) != 1)
  38.         return -1 ;
  39.  
  40.     return 0 ;
  41. }
  42.  
  43. /* Convert a host-format net/rom level 3 header into an mbuf ready
  44.  * for transmission.
  45.  */
  46.  
  47. struct mbuf *
  48. htonnr3(hdr)
  49. register struct nr3hdr *hdr;
  50. {
  51.     struct mbuf *rbuf ;
  52.     register char *cp ;
  53.     char *putaxaddr();
  54.  
  55.     if (hdr == (struct nr3hdr *) NULL)
  56.         return NULLBUF ;
  57.  
  58.     /* Allocate space for return buffer */
  59.     if ((rbuf = alloc_mbuf(NR3HLEN)) == NULLBUF)
  60.         return NULLBUF ;
  61.  
  62.     rbuf->cnt = NR3HLEN ;
  63.  
  64.     /* Now convert */
  65.     cp = rbuf->data ;
  66.  
  67.     hdr->source.ssid &= ~E ;    /* source E-bit is always off */
  68.     hdr->dest.ssid |= E ;        /* destination E-bit always set */
  69.  
  70.     cp = putaxaddr(cp,&hdr->source) ;
  71.     cp = putaxaddr(cp,&hdr->dest) ;
  72.     *cp = hdr->ttl ;
  73.  
  74.     return rbuf ;
  75. }
  76.  
  77. /* Convert a net/rom routing broadcast destination subpacket from
  78.  * network format to a host format structure.  Return -1 if error,
  79.  * 0 if OK.
  80.  */
  81. int ntohnrdest(ds,bpp)
  82. register struct nr3dest *ds ;
  83. struct mbuf **bpp ;
  84. {
  85.     char buf[AXALEN] ;
  86.     char quality ;
  87.  
  88.     /* get destination callsign */
  89.     if (pullup(bpp,buf,AXALEN) < AXALEN)
  90.         return -1 ;
  91.     memcpy(ds->dest.call,buf,ALEN) ;
  92.     ds->dest.ssid = buf[ALEN] ;
  93.  
  94.     /* get destination alias */
  95.     if (pullup(bpp,ds->alias,ALEN) < ALEN)
  96.         return -1 ;
  97.     ds->alias[ALEN] = '\0' ;
  98.  
  99.     /* get best neighbor callsign */
  100.     if (pullup(bpp,buf,AXALEN) < AXALEN)
  101.         return -1 ;
  102.     memcpy(ds->neighbor.call,buf,ALEN) ;
  103.     ds->neighbor.ssid = buf[ALEN] ;
  104.  
  105.     /* get route quality */
  106.     if (pullup(bpp,&quality,1) < 1)
  107.         return -1 ;
  108.     ds->quality = uchar(quality) ;
  109.  
  110.     return 0 ;
  111. }
  112.  
  113. /* Convert a host-format net/rom destination subpacket into an
  114.  * mbuf ready for transmission as part of a route broadcast
  115.  * packet.
  116.  */
  117. struct mbuf *
  118. htonnrdest(ds)
  119. register struct nr3dest *ds ;
  120. {
  121.     struct mbuf *rbuf ;
  122.     register char *cp ;
  123.  
  124.     if (ds == (struct nr3dest *) NULL)
  125.         return NULLBUF ;
  126.  
  127.     /* Allocate space for return buffer */
  128.     if ((rbuf = alloc_mbuf(NRRTDESTLEN)) == NULLBUF)
  129.         return NULLBUF ;
  130.  
  131.     rbuf->cnt = NRRTDESTLEN ;
  132.  
  133.     cp = rbuf->data ;
  134.  
  135.     memcpy(cp,ds->dest.call,ALEN) ;
  136.     cp += ALEN ;
  137.     *cp++ = ds->dest.ssid ;
  138.  
  139.     memcpy(cp,ds->alias,ALEN) ;
  140.     cp += ALEN ;
  141.  
  142.     memcpy(cp,ds->neighbor.call,ALEN) ;
  143.     cp += ALEN ;
  144.     *cp++ = ds->neighbor.ssid ;
  145.  
  146.     *cp = uchar(ds->quality) ;
  147.  
  148.     return rbuf ;
  149. }
  150.  
  151.